﻿using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
namespace SpeedSearch
{
    public static class ExcelExportWrapper
    {
        public static void ExportSelectionToExcel(IList selection)
        {
            if (!Directory.Exists(@"C:\Temp"))
                Directory.CreateDirectory(@"C:\Temp");

            using (var writer = new StreamWriter(String.Format(@"C:\Temp\speedSearchResults_{0}_{1}.csv",
                DateTime.Now.ToString("yyyyMMdd"), DateTime.Now.ToString("hhmmss"))))
            {
                writer.WriteLine("Nom;Extension;Emplacement;Taille (KB);Derniere modification");
                foreach (ListViewItem item in selection)
                {
                    writer.WriteLine(String.Format("{0};{1};{2};{3};{4}",
                        item.SubItems[0].Text, item.SubItems[1].Text, item.SubItems[2].Text,
                        item.SubItems[3].Text, item.SubItems[4].Text));
                }
                writer.Close();
            }

            using (Process newProcess = new Process())
            {
                var fileName = String.Format(@"C:\Temp\speedSearchResults_{0}_{1}.csv",
                    DateTime.Now.ToString("yyyyMMdd"), DateTime.Now.ToString("hhmmss"));
                newProcess.StartInfo.Arguments = fileName;
                newProcess.StartInfo.FileName = "excel";

                try
                {
                    newProcess.Start();
                }
                catch
                {
                    MessageBox.Show(String.Format("Echec lors de l'ouverture de {0}", fileName));
                }
            }
        }
    }
}
